<template>
    <div>
      <h2>用药信息</h2>
      <div>
        医生名称:<input type="text" v-model="query.doctorname"/>
        科室名称:
        <select v-model="query.diffname">
            <option value="">请选择</option>
            <option :value="item.differentName" v-for="item in dif">{{ item.differentName }}</option>
        </select>
        <input type="button" value="查询" class="btn btn-primary" @click="showw"/>
        <input type="button" value="批量完成" class="btn btn-success" @click="updateState(0)"/>
      </div>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td>
                <input type="checkbox" v-model="checkall" @click="All"/>
            </td>
            <td>id</td>
             <td>药品名称</td>
             <td>科室</td>
             <td>用药日期</td>
             <td>病人姓名</td>
             <td>病房号码</td>
             <td>用药状态</td>
             <td>配药医生</td>
             <td>操作</td>
          </tr>
          <tr v-for="item in data.list">
            <td>
                <input type="checkbox" :value="item.serciesId" v-model="checkone" @change="One"/>
            </td>
            <td>{{ item.serciesId }}</td>
             <td>{{item.serciesName}}</td>
             <td>{{item.differentName}}</td>
             <td>{{moment(item.useTime).format("YYYY-MM-DD")}}</td>
             <td>{{item.userName}}</td>
             <td>{{item.serNumber}}</td>
             <td>{{item.state==0?"已完成":"未开始"}}</td>
             <td>{{item.doctorName}}</td>
             <td>
                <input type="button" value="删除" class="btn btn-danger" @click="del(item.serciesId)"/>
                <input type="button" value="修改" class="btn btn-info" @click="Update(item.serciesId)"/>
             </td>
          </tr>
        </tbody>
      </table>
      <div>
        <span>共{{ data.totalCount }}条</span>
        <span>每页{{ query.pageSize }}条</span>
        <span>当前第<b style="color: red;">{{ query.pageIndex }}</b>页/共{{ data.totalPage }}页</span>
        <input type="button" value="首页" @click="Gopage(1)"/>
        <input type="button" value="上一页" @click="Gopage(query.pageIndex-1)"/>
        <span v-for="item in data.totalPage" @click="Gopage(item)">{{ item }}</span>
        <input type="button" value="下一页" @click="Gopage(query.pageIndex+1)"/>
        <input type="button" value="尾页" @click="Gopage(data.totalPage)"/>
      </div>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { ref,reactive,onMounted } from 'vue';
import { useRoute,useRouter } from 'vue-router';
const router=useRouter();
const route=useRoute();


onMounted(()=>{
    showdif();
    showlist();
})

let dif:any=ref([])

const showdif=()=>
{
  axios
  ({
    url:'/api/Diff/ShowDiff',
    method:'get'
  })
  .then(res=>{
    dif.value=res.data;
  })
  .catch(err=>{

  })
}




let query:any=reactive
({
    pageIndex:1,
    pageSize:3,
    doctorname:'',
    diffname:''
})

let data:any=reactive
({
    totalCount:'',
    totalPage:'',
    list:[]
})

const showw=()=>
{
    query.pageIndex=1;
    showlist();
}
//显示用药信息列表
const showlist=()=>
{
    axios
  ({
    url:'/api/Diff/ShowSer',
    method:'get',
    params:query
  })
  .then(res=>{
    Object.assign(data,res.data);
  })
  .catch(err=>{

  })
}
//分页
const Gopage=(num:any)=>
{
    if(num<1)
    {
        query.pageIndex=1;
        return;
    }
    if(num>data.totalPage)
    {
        query.pageIndex=data.totalPage;
        return;
    }
    query.pageIndex=num;
    showlist();
}

//删除
const del=(id:any)=>
{
    let f=confirm("确认删除吗?")
    {
        if(f)
        {
            axios
            ({
                url:'/api/Diff/SerDelete',
                method:'delete',
                params:
                {
                    id:id
                }
            })
            .then(res=>{
                if(res.data>0)
                {
                    alert("删除成功");
                    showlist();
                    return;
                }
                else
                {
                    alert("删除失败");
                    return;
                }
            })
            .catch(err=>{

            })
        }
    }
}

//全选/取消
let checkall=ref(false)
let checkone=ref([])

const All=()=>
{
    checkall.value=!checkall.value;
    if(checkall.value==true)
    {
        checkone.value=data.list.map((item:any)=>{
            return item.serciesId;
        })
    }
    else
    {
        checkone.value=[];
    }
}

const One=()=>
{
    if(checkone.value.length==data.list.length)
    {
        checkall.value=true;
    }
    else
    {
        checkall.value=false;
    }
}

//批量完成
const updateState=(state:any)=>
{
    let f=confirm("确认修改吗?")
    {
        if(f)
        {
            axios
            ({
                url:'/api/Diff/UpdateState',
                method:'put',
                params:
                {
                    ids:checkone.value.toString(),
                    state:state
                }
            })
            .then(res=>{
                if(res.data>0)
                {
                    alert("修改成功");
                    checkall.value=false;
                    checkone.value=[];
                    showlist();
                    return;
                }
                else
                {
                    alert("修改失败");
                    checkall.value=false;
                    checkone.value=[];
                    return;
                }
            })
            .catch(err=>{

            })
        }
    }
}

//修改跳转
const Update=(id:any)=>
{
    router.push
    ({
        path:'/serUpdate',
        query:
        {
            id:id
        }
    })
}


</script>

<style scoped>
span
{
    margin: 0 10px;
}
input
{
    margin: 0 10px;
}
</style>